﻿using S7.Net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using thinger.HeatingControlProject._4.实体类;

namespace thinger.HeatingControlProject._2.数据层
{
    /// <summary>
    /// PLC信息数据类
    /// </summary>
    public class PlcInfoService
    {
        /// <summary>
        /// 读取ini配置文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns>PLCInfo对象</returns>
        public PlcInfo GetPlcInfoByPath(string path)
        {
            PlcInfo info = new PlcInfo();
            //读取ini文件，并封装到对象中
            if(!File.Exists(path))
            {
                return null;
            }
            info.IPAddress = IniConfigHelper.ReadIniData("PLC信息", "IP地址", "127.0.0.1", path);
            info.CpuType = (CpuType)Enum.Parse(typeof(CpuType), IniConfigHelper.ReadIniData("PLC信息", "CPU类型", "S7200smart", path),true);
            info.Rack = Convert.ToInt16(IniConfigHelper.ReadIniData("PLC信息", "机架号", "0", path));
            info.Slot = Convert.ToInt16(IniConfigHelper.ReadIniData("PLC信息", "插槽号", "0", path));
            return info;
        }
        /// <summary>
        /// 写入PlcInfo对象到ini文件
        /// </summary>
        /// <param name="info"></param>
        /// <param name="path"></param>
        /// <returns>成功与否</returns>
        public bool SetPlcInfoToPath(PlcInfo info,string path)
        {
            bool result = true;
            result &= IniConfigHelper.WriteIniData("PLC信息", "IP地址", info.IPAddress, path);
            result &= IniConfigHelper.WriteIniData("PLC信息", "CPU类型", info.CpuType.ToString(), path);
            result &= IniConfigHelper.WriteIniData("PLC信息", "机架号", info.Rack.ToString(), path);
            result &= IniConfigHelper.WriteIniData("PLC信息", "插槽号", info.Slot.ToString(), path);

            return result;

        }
    }
}
